package org.railage.server.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.railage.server.entities.TicketOfUser;
import org.railage.server.util.HibernateUtil;

public class TicketOfUserDAOImpl implements TicketOfUserDAO {

	private static final Logger log = Logger
			.getLogger(TicketOfUserDAOImpl.class);

	@SuppressWarnings("unchecked")
	public List<TicketOfUser> getAllTicketsOfUser(int userID) {
		Session session = null;
		List<TicketOfUser> tickets = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.getNamedQuery("getTicketsOfUser");
			query.setInteger("userID", userID);
			tickets = (List<TicketOfUser>) query.list();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return tickets;
	}

}
